
// 查看积分账户轨迹
bangkeapp.controller('interestsaccountCtrl', 
	["$scope", "$state", "userinfoService", "accountService", "$ionicModal", 
		"$stateParams", "$ionicLoading", "commonService", "fileurl", 
		"$ionicPopover", "interestsaccountService", "$timeout",
    function($scope, $state, userinfoService, accountService, $ionicModal, 
    		$stateParams, $ionicLoading,  commonService, fileurl, 
    		$ionicPopover, interestsaccountService, $timeout){
    		// init
    		$scope.member = userinfoService.getUser();
    		$scope.account = {};
    		$scope.account.type = "1";
    		$scope.account.amount = "0";
    		$scope.advancerecharge = {};
    		$scope.fileurl = fileurl;
    				
    		commonService.getbankinfo().then(function(result) {
	         $scope.inbank = result.body.inbank;
        }, function(error) {
        }, function(progress) {
        });
    		
    		$scope.redpacket = function(){
    			var hideflag = [false, false];
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
    			// 红包总额
			accountService.redpacket().then(function(result) {
	            $scope.account.amount = result.redpacket;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[0] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
	        
			// 红包轨迹
    			accountService.tarjectory("01,07,11,12,17,18,19,20,21,22,23,24,25").then(function(result) {
	            $scope.account.tarjectory = result;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[1] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
    		}
    		$scope.interests = function(){
    			var hideflag = [false, false];
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
    			// 股权总额
    			accountService.interests().then(function(result) {
	            $scope.account.amount = result.interests;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[0] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
	        
			// 股权轨迹
    			accountService.tarjectory("02").then(function(result) {
	            $scope.account.tarjectory = result;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[1] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
    		}
    		$scope.bonus = function(){
    			var hideflag = [false, false];
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
    			// 红利总额
    			accountService.bonus().then(function(result) {
	            $scope.account.amount = result.bonus;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[0] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
	        
			// 红利轨迹
    			accountService.tarjectory("08,09,10").then(function(result) {
	            $scope.account.tarjectory = result;
	        }, function(error) {
	            $scope.list = "error!";
	        }, function(progress) {
	        		hideflag[1] = true;
	        		if (hideflag[0] && hideflag[1]) {
	        			$ionicLoading.hide();
	        		}
	        });
    		}
    		
    		// init
    		angular.element(document.querySelectorAll(".accounttab")).removeClass("active");
    		if (undefined != $stateParams.type && "1" == $stateParams.type) {
    			$scope.account.type = "1";
    			$scope.account.name = "红包";
    			angular.element(document.querySelectorAll("#redpacket")).addClass("active");
    			$scope.redpacket();
    		} else if (undefined != $stateParams.type && "2" == $stateParams.type) {
    			$scope.account.type = "2";
    			$scope.account.name = "股权";
    			angular.element(document.querySelectorAll("#interests")).addClass("active");
    			$scope.interests();
    		} if (undefined != $stateParams.type && "3" == $stateParams.type) {
    			$scope.account.type = "3";
    			$scope.account.name = "红利";
    			angular.element(document.querySelectorAll("#bonus")).addClass("active");
    			$scope.bonus();
    		}
    		
    		$scope.change = function(){
    			var this_ = angular.element(event.target)
    			angular.element(document.querySelectorAll(".accounttab")).removeClass("active");
    			this_.addClass("active");
    			$scope.account.tab = this_.attr("data-tab");
    			
    			if ("1" == $scope.account.tab) {
    				$scope.account.name="红包";
    				$scope.account.type = "1";
    				$scope.redpacket();
    			}
    			if ("2" == $scope.account.tab) {
    				$scope.account.name="股权";
    				$scope.account.type = "2";
    				$scope.interests();
    			}
    			if ("3" == $scope.account.tab) {
    				$scope.account.name="红利";
    				$scope.account.type = "3";
    				$scope.bonus();
    			}
    		}
    		
    		// 查看详细模块
        $ionicModal.fromTemplateUrl('viewaccountdetail.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.viewaccountdetailmodal = modal;
            $scope.member = userinfoService.getUser();
            $scope.closeviewaccountdetail = function(){
            		$scope.viewaccountdetailmodal.hide();
            }
        });
        
        // 查看详细
    		$scope.toviewdetaile = function(item){
    			$scope.viewaccountdetailmodal.show();
    			$scope.detail = item;
    		}
    		
    		$scope.torechargeredpacket = function(){
    			// 红包积分充值
	        $ionicModal.fromTemplateUrl('templates/member/account/redpacketrecharge.html', {
	            scope: $scope
	        }).then(function(modal) {
	            $scope.redpacketrechargemodal = modal;
	            $scope.redpacketrechargemodal.show();
	            
	            $scope.advancerecharge = {};
	            $scope.advancerecharge.incardimg = $scope.inbank[0].incardimg;
		    		$scope.advancerecharge.inaccountbank = $scope.inbank[0].inaccountbank;
				$scope.advancerecharge.inaccountname = $scope.inbank[0].inaccountname;
				$scope.advancerecharge.inbranche = $scope.inbank[0].inbranche;
				$scope.advancerecharge.incardno = $scope.inbank[0].incardno;
				$scope.advancerecharge.rechargetype = '1'; // 充值类型: 红包积分
	            
		        $scope.closeredpacketrechargemodal = function(){
		        		$scope.redpacketrechargemodal.hide();
		        }
	        });
    		}
    		
    		$scope.toupload = function(){
    			// 红包积分充值
	        $ionicModal.fromTemplateUrl('templates/member/account/upload.html', {
	            scope: $scope
	        }).then(function(modal) {
	            $scope.uploadmodal = modal;
	            $scope.uploadmodal.show();
			    
	            // 上传组件开始
				$scope.upload = function(){
					
					var sidebar = document.getElementsByClassName('file-hook');
					angular.element(sidebar)[0].click();
				};
				
				$scope.previewimag = function() {
					
					var base64 = new UploadBase64(event.target, angular.element
						(document.getElementsByClassName('img-list'))[0], 'redpacketrechargeimg',
						userinfoService.getUser().memberid, 'interestsaccountCtrl');
					
				}
			
				$scope.deleteimg = function() {
				    angular.element(event.target).parent().remove();
				    angular.element(document.querySelector('.img-uploaded'))
						.removeClass("img-uploaded").addClass("img-upload");
				}
				$scope.submitimg = function() {
					$scope.uploadmodal.hide();
				}
				// 上传组件结束
				
				$scope.goback = function() {
					$scope.uploadmodal.hide();
				}
	        });
    		}
		
    		$scope.tochooseinbank = function(){
    			$ionicPopover.fromTemplateUrl('templates/member/account/paytype.html', {
				scope: $scope
			}).then(function(popover) {
				$scope.paytypepopover = popover;
				$scope.paytypepopover.show();
			});
    			
    		}
    		$scope.chooseinbank = function(inbankinfo){
    			$scope.paytypepopover.remove();
    			$scope.advancerecharge.inaccountbank = inbankinfo.inaccountbank;
    			$scope.advancerecharge.inaccountname = inbankinfo.inaccountname;
    			$scope.advancerecharge.inbranche = inbankinfo.inbranche;
    			$scope.advancerecharge.incardno = inbankinfo.incardno;
    		}
    		
    		// 提交申请
    		$scope.tosubmit = function() {
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			// 提交前校验开始
			// 红包amount不能为空
			if (undefined == $scope.advancerecharge.amount || '' == $scope.advancerecharge.amount) {
				
				return false;
			}
//			// 红包amount不能为空
//			if (undefined == $scope.advancerecharge.amount || '' == $scope.advancerecharge.amount) {
//				
//			}
			// 提交前校验结束
    			interestsaccountService.saveadvancerecharge($scope.advancerecharge).then(function(result) {
    				
    				$ionicLoading.hide();
    				$scope.saveresult = result.body;
    				
    				if ('1' == $scope.saveresult.result) {
    					$scope.redpacketrechargemodal.remove();
    				}
	            // 充值结束
		        $ionicModal.fromTemplateUrl('templates/member/account/rechargeresult.html', {
		            scope: $scope
		        }).then(function(modal) {
		            $scope.rechargeresultmodal = modal;
		            $scope.rechargeresultmodal.show();
		            $scope.closerechargeresultmodal = function(){
		            		$scope.rechargeresultmodal.remove();
		            }
		            $scope.complate = function(){
		            		$scope.rechargeresultmodal.remove();
		            }
		        });
	        }, function(error) {
	        }, function(progress) {
	        });
    		}
    		
    		// 充值进度查询
    		$scope.toschedule = function(){
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
    			// 充值结束
	        $ionicModal.fromTemplateUrl('templates/member/account/redpacketrechargelist.html', {
	            scope: $scope
	        }).then(function(modal) {
	        		var hasmore = true;
	        		$scope.redpacketrechargelist = {};
	        		$scope.redpacketrechargelist.advancerecharge = {};
	        		$scope.redpacketrechargelist.advancerecharge.memberid = userinfoService.getUser().memberid;
	        		$scope.redpacketrechargelist.page = 1;
	        		$scope.redpacketrechargelist.pagesize = 10;
	        		$scope.advancerechargelist = [];
	        		
	        		$scope.getrechargelist = function() {
	        			// 进度查询
		        		interestsaccountService.queryrechargeinfo
		        			($scope.redpacketrechargelist.advancerecharge, $scope.redpacketrechargelist.page,
		        				$scope.redpacketrechargelist.pagesize).then(function(result) {
		        					
		        			angular.forEach(result.body.advancerecharge, function(value){
	                        $scope.advancerechargelist.push(value);
	                    	});
	                    	$scope.$broadcast('scroll.infiniteScrollComplete');
	                    	$scope.redpacketrechargelist.page++;
		        			if ($scope.advancerechargelist.length == result.body.no) {
		        				hasmore = false;
		        			}
			        }, function(error) {
			        }, function(progress) {
			        		$ionicLoading.hide();
			        });
	        		}
	        		
	            $scope.redpacketrechargelistmodal = modal;
	            $scope.redpacketrechargelistmodal.show();
	            
	            $scope.closeredpacketrechargelistmodal = function(){
	            		$scope.redpacketrechargelistmodal.remove();
	            }
	            
		        //上拉触发函数
		        $scope.loadMore = function () {
		            //这里使用定时器是为了缓存一下加载过程，防止加载过快
		            $timeout(function () {
		                
		                if (!hasmore) {
		                    $scope.$broadcast('scroll.infiniteScrollComplete');
		                    return;
		                }
		                $scope.getrechargelist();
		            }, 500);
		        };
		        
		        // 控制列表是否允许其加载更多
		        $scope.moreDataCanBeLoaded = function () {
		            return hasmore;
		        }
	            
	        });
    		}
    		$scope.todetail = function(advancerecharge){
    			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
    			// 查看充值明细
	        $ionicModal.fromTemplateUrl('templates/member/account/redpacketrechargedetail.html', {
	            scope: $scope
	        }).then(function(modal) {
	        		
	        		$scope.redpacketrechargedetail = {};
	        		$scope.redpacketrechargedetail.advancerecharge = {};
	        		$scope.redpacketrechargedetail.advancerecharge.id = advancerecharge.id;
	        		$scope.redpacketrechargedetail.advancerecharge.memberid = userinfoService.getUser().memberid;
	        	
	        		// 进度查询
	        		interestsaccountService.queryrechargeinfo
	        			($scope.redpacketrechargedetail.advancerecharge).then(function(result) {
	        			$scope.detail = result.body.advancerecharge[0];
    					$scope.detail.applyimg = fileurl + $scope.detail.applyimg;
		        }, function(error) {
		        }, function(progress) {
		        		$ionicLoading.hide();
		        });
	        		
	            $scope.detailmodal = modal;
	            $scope.detailmodal.show();
	            
	            $scope.closedetailmodal = function(){
	            		$scope.detailmodal.remove();
	            }
	        });
    		}
    		
    		// 账户说明
    		$scope.paytypeexplain = function(){
    			// 充值结束
	        $ionicModal.fromTemplateUrl('templates/member/account/paytypeexplain.html', {
	            scope: $scope
	        }).then(function(modal) {
	        		
	            $scope.paytypeexplainmodal = modal;
	            $scope.paytypeexplainmodal.show();
	            
	            $scope.closepaytypeexplainmodal = function(){
	            		$scope.paytypeexplainmodal.remove();
	            }
	        });
    		}
    		
    		/**
    		 * 压缩图片后回调函数, 名字不可更改
    		 * @param {Object} filebase64
    		 * @param {Object} name
    		 */
    		$scope.uploadfile = function(filebase64, name){
    			
			// 加载动画
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			var imgData = filebase64;
			$scope.img = {};
			// 调用接口上传
			commonService.uploadfile(filebase64.substring(22, filebase64.length), name).then(function(result) {
				if ("M" == result.header.ResponseCode) {
					alert("上传成功!");
					$scope.img.fileimg = imgData;
					$scope.advancerecharge.applyimg = result.body.filename;
					angular.element(document.querySelector('.img-upload')).removeClass("img-upload").addClass("img-uploaded");
					return true;
				} else {
					alert(result.body.msg);
					return false;
				}
	        }, function(error) {
	        }, function(progress) {
	        		$ionicLoading.hide();
	        });
    		}
    }
]);